package com.sunnada.edu.system.pub.util;

 

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期函数类 
 * 2014-11-07 chenbin7270
 */
public class DateUtil {

	public static String datetimeFormat = "yyyy-MM-dd HH:mm:ss";
	public static String dateFormat = "yyyy-MM-dd";
	public static String yearmonthFormat = "yyyy-MM";
	public static String monthFormat = "MM";
	public static String timeSuffix1 = " 00:00:00";
	public static String timeSuffix2 = " 23:59:59";
	public static String secondSuffix1 = ":00";
	public static String secondSuffix2 = ":59";

	public static SimpleDateFormat simpleDateFormat(String format) {
		return new SimpleDateFormat(format);
	}

	public static SimpleDateFormat simpleDateFormat() {
		return new SimpleDateFormat(datetimeFormat);
	}

	/**
	 * 返回与当天相隔n天的日期
	 * 
	 * @param date
	 * @param amount
	 * @return
	 */
	public static Date getDay(Date date, int n) {
		try {
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			cal.add(Calendar.DATE, n);
			return cal.getTime();
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 返回当前月第一天的日期
	 * 
	 * @param date
	 * @return
	 */
	public static Date getFirstDayOfMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DATE, 1);
		return calendar.getTime();
	}

	/**
	 * 返回当前月最后一天的日期
	 * 
	 * @param date
	 * @return
	 */
	public static Date getLastDayOfMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DATE, 1);
		calendar.roll(Calendar.DATE, -1);
		return calendar.getTime();
	}

	/**
	 * 返回与当前日期时间相隔n小时的日期时间
	 * 
	 * @param date
	 * @param n
	 * @return
	 */
	public static Date getHour(Date date, int n) {
		try {
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			cal.add(Calendar.HOUR, n);
			return cal.getTime();
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 返回与当前日期时间相隔n个月的日期时间
	 * 
	 * @param date
	 * @param n
	 * @return
	 */
	public static Date getMonth(Date date, int n) {
		try {
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			cal.add(Calendar.MONTH, n);
			return cal.getTime();
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 返回当前月考勤数据的日期范围 <功能详细描述>
	 * 
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	public static String[] getNowDateRange() {
		String days[] = new String[2];

		Date date = new Date();
		Calendar calendar1 = Calendar.getInstance();
		calendar1.setTime(date);
		calendar1.set(Calendar.DATE, 21);
		calendar1.roll(Calendar.MONTH, -2);
		days[0] = new SimpleDateFormat(dateFormat).format(calendar1.getTime());

		Calendar calendar2 = Calendar.getInstance();
		calendar2.setTime(date);
		calendar2.set(Calendar.DATE, 20);
		days[1] = new SimpleDateFormat(dateFormat).format(calendar2.getTime());

		return days;
	}

	
	/**
	 * 获得正确的 时间
	 * 
	 * @return yyyyMMddHHmmsss
	 */
	public static String getCurrentTime() {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmsss");
		//sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
		String time = sdf.format(date);
		return time;

	}
	
	public static String getCurrentTime1() {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		//sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
		String time = sdf.format(date);
		return time;

	}
	public static String getCurrentTime1(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String time = sdf.format(date);
		return time;

	}
	
	public static String getCurrentTime2() {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
		//sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
		String time = sdf.format(date);
		return time;

	}
	
	public static String getCurrentTime3() {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
		//sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
		String time = sdf.format(date);
		return time;

	}

	/**
	 * 字符串转日期
	 * 
	 * @return yyyyMMddHHmmsss
	 * @throws ParseException
	 */
	public static String getDateToString(Date date) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("dateFormat");
		//sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
		String time = sdf.format(date);
		return time;

	}

	/**
	 * 获得当前的时间
	 * 
	 * @return yyyy-MM-dd HH:mm:ss
	 */
	public static String getNowTime() {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat(datetimeFormat);
		//sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
		String time = sdf.format(date);
		return time;
	}

	/**
	 * 获得当前的日期
	 * 
	 * @return yyyy-MM-dd
	 */
	public static String getNowDate() {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
		//sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
		String time = sdf.format(date);
		return time;
	}

	/**
	 * 获得与当前的月份相差n的日期
	 * 
	 * @return yyyy-MM-dd
	 */
	public static String getDateLimit(Integer n) {
		try {
			Date date = new Date();
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			cal.add(Calendar.MONTH, n);
			return simpleDateFormat(dateFormat).format(cal.getTime());
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 获得传入日期的月份相差n的日期
	 * 
	 * @return yyyy-MM-dd
	 */
	public static String getDateLimit(Integer n, String d) {
		try {
			Date date = simpleDateFormat(dateFormat).parse(d);
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			cal.add(Calendar.MONTH, n);
			return simpleDateFormat(dateFormat).format(cal.getTime());
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 换名称
	 * 
	 * @param filename model.xlsx
	 * @return 1231312313123.xlsx
	 */
	public static String getNewFileName(String filename) {
		String temp = DateUtil.getCurrentTime();
		String newFilename = temp + filename.substring(filename.lastIndexOf("."));
		return newFilename;
	}

	/**
	 * 获得当月第一天的日期
	 * 
	 * @param
	 * @return 2012
	 */
	public static String getFirstMonth() {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DATE, 1);
		Date DateTime = calendar.getTime();
		String sTime = dateFormat.format(DateTime);
		return sTime;
	}

	/**
	 * 获得当月最后一天的日期
	 * 
	 * @param
	 * @return 2012
	 */
	public static String getEndMonth() {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DATE, 1);
		calendar.roll(Calendar.DATE, -1);
		Date endTime = calendar.getTime();
		String eTime = dateFormat.format(endTime);

		return eTime;
	}

	/**
	 * 获得传入月份的第一天的日期
	 * 
	 * @param
	 * @return 2012
	 */
	public static String getFirstMonth(String month) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date date = dateFormat.parse(month + "-01");
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.set(Calendar.DATE, 1);
			Date DateTime = calendar.getTime();
			String sTime = dateFormat.format(DateTime);
			return sTime;
		} catch (ParseException e) {
			return null;
		}
	}

	/**
	 * 获得传入月份最后一天的日期
	 * 
	 * @param
	 * @return 2012
	 */
	public static String getEndMonth(String month) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date date = dateFormat.parse(month + "-01");
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.set(Calendar.DATE, 1);
			calendar.roll(Calendar.DATE, -1);
			Date endTime = calendar.getTime();
			String eTime = dateFormat.format(endTime);

			return eTime;
		} catch (ParseException e) {
			return null;
		}
	}

	public static String getNowAtteMonth(String d) throws ParseException {
		Date date = simpleDateFormat(dateFormat).parse(d);
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);

		if (cal.get(Calendar.DATE) > 20) {
			cal.add(Calendar.MONTH, 1);
		}

		String str = simpleDateFormat(yearmonthFormat).format(cal.getTime());

		return str;
	}

	
	/**
	 * @param args
	 * @throws ParseException
	 */
	public static void main(String[] args) throws ParseException {
	/*   String s=	getCurrentTime3();
	   String ss="4G";
	    ss=ss.substring(0,1);
	    */
        //  String l= addZeroForNum("1", 8);
          
		//String ss = DateUtil.getDateLimit(7);
		//System.out.println(l);
	}

}
